const db = wx.cloud.database()
const backgroud = db.collection('notes-bg')
//添加背景图片
async function addBackground(fileId) {
  //获取服务端时间
  const createTime = db.serverDate()

  return await backgroud.add({
    data: {
      fileId: fileId,
      createTime: createTime,
      selectTime: createTime
    }
  })
}

//获取
async function getBackground(skip,limit) {
  return await backgroud
    .orderBy('selectTime', 'desc')
    .orderBy('createTime', 'desc')
    .skip(skip)
    .limit(limit)
    .get()
}
//图片个数
async function getBackgroundCount(){
  return await backgroud.count()
}
//删除
async function delBackground(id, fileID) {
  wx.cloud.deleteFile({
    fileList: [fileID]
  })
  return await backgroud.doc(id).remove()
}

//选择
async function selectBackground(id) {
  const selectTime = db.serverDate()
  return await backgroud.doc(id).update({
    data: {
      selectTime: selectTime
    }
  }).then(() => backgroud.doc(id).get())
}

export {
  addBackground,
  getBackground,
  delBackground,
  selectBackground,
  getBackgroundCount
}